-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

newTalentType{ type="combat/firearms-basic", name = "firearms", description = "Basic firearm techniques", generic = true }

newTalent{
	name = "Shoot",
	type = {"combat/firearms-basic", 1},
	points = 5,
	range = function(self, t)
		local weapon, ammo = self:hasGunWeapon()
		if not weapon then return 0 end
		return weapon.combat.range * ammo.combat.range_mod
	end,
	message = "@Source@ shoots!",
	requires_target = true,
	targets = function(self, t)
		local weapon, ammo = self:hasGunWeapon()
		return {
			-- Target for the firearm
			{
				type="ballbolt",
				range=self:getTalentRange(t),
				radius=ammo.combat.radius or 0,
				talent=t,
				speed=weapon.combat.speed * ammo.combat.speed_mod,
				angle_fudge=weapon.combat.accuracy * ammo.combat.accuracy_mod,
				display=ammo.projectile_display,
				canClimb = function(self, terrain)
					if not terrain.climbable then return false end
					if terrain.climbable.difficulty > 0 then return false end
					return true  -- Does not return a number because Projectile does not have setEffect (look at Grid:block_move)
				end,
				-- A custom block_path so that the projectile can be passed to checkEntity and "climb" over terrain
				block_path = function(typ, lx, ly)
					if not typ.no_restrict then
						if typ.requires_knowledge and not game.level.map.remembers(lx, ly) and not game.level.map.seens(lx, ly) then return true end
						if not typ.pass_terrain and game.level.map:checkEntity(lx, ly, engine.Map.TERRAIN, "block_move", typ) then return true
						-- If we explode do to something other than terrain, then we should explode ON the tile, not before it
						elseif typ.stop_block and game.level.map:checkAllEntities(lx, ly, "block_move", typ) then return true, lx, ly end
						if typ.range and typ.source_actor and math.sqrt((typ.source_actor.x-lx)^2 + (typ.source_actor.y-ly)^2) > typ.range then return true end
					end
				end,
				block_radius = function(typ, lx, ly)
					return not typ.no_restrict and game.level.map:checkEntity(lx, ly, engine.Map.TERRAIN, "block_move", typ)
				end,
			}
		}
	end,
	on_preuse = function(self, t, silent)
		local weapon, ammo = self:hasGunWeapon()
		if not weapon then
			if not silent then
				game.logPlayer(self, "You cannot shoot: %s", ammo)
			end
			return false
		else
			return true
		end
	end,
	action = function(self, t)
		local weapon, ammo = self:hasGunWeapon()
		ammo_radius = ammo.combat and ammo.combat.radius or 0

		local targets = self:resolveTalentTargets(t)
		local tg = targets[1].typ
		local x = targets[1].x
		local y = targets[1].y
		if not x or not y then return false end
		return self:firearmShoot(tg, x, y)
	end,
	info = function(self, t)
		return ([[Shoot your gun!  Your training allows you to shoot at a range of %d, but your accuracy falls off as you reach your maximum range.]]):format(self:getTalentRange(t))
	end,
}

newTalent{
    name = "Headshot",
    type = {"combat/firearms-basic", 2},
    points = 5,
    mode = "passive",
    info = function(self, t)
        return ([[Increases the odds of a critical headshot to %.1f, and the damage multiplier to %.1f]]):format(10 + self:getTalentLevel(Talents.T_HEADSHOT) * 5 / 3,
                                                                           1.5 + self:getTalentLevel(Talents.T_HEADSHOT) / 3)
    end,
}